package com.mb.prod.service;

import com.mb.common.pojo.Product;
import com.mb.common.utils.MapperUtil;
import com.mb.common.vo.EasyUIResult;
import com.mb.prod.mapper.ProdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class ProdService {

    @Autowired(required = false)
    private ProdMapper prodMapper;



    //分页查询
    public EasyUIResult queryByPage(Integer page, Integer rows) {
        EasyUIResult result=new EasyUIResult();
        int total=prodMapper.selectCount();
        result.setTotal(total);
        //从page计算start
        int start=(page-1)*rows;
        List<Product> products=prodMapper.selectProductByPage(start,rows);
        result.setRows(products);
        return result;
    }




    //根据id查单个商品
    @Autowired
    private StringRedisTemplate redisTemplate;
    public Product queryOneProduct(String productId) {
        ValueOperations<String, String> opsForString = redisTemplate.opsForValue();
        String productKey="product_"+productId;
        try {
            //判断命中
            if (redisTemplate.hasKey(productKey)) {
                String pJson = opsForString.get(productKey);
                Product product = MapperUtil.MP
                        .readValue(pJson, Product.class);
                return product;
            } else {
                //缓存没数据,从数据库查询
                Product p = prodMapper.selectProductById(productId);
                String pJson = MapperUtil.MP.writeValueAsString(p);
                opsForString.set(productKey,pJson,2,
                        TimeUnit.DAYS);
                return p;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    //新增商品
    public void addProduct(Product product) {
        //TODO 主动缓存 redis
        //UUID
        String productId= UUID.randomUUID().toString();
        System.out.println("新增UUID:"+productId);
        product.setProductId(productId);
        //主动缓存
        try{
            String pJson=MapperUtil.MP.writeValueAsString(product);
            String productKey="product_"+productId;
            redisTemplate.opsForValue().set(productKey,pJson,2,TimeUnit.DAYS);
        }catch (Exception e){
            e.printStackTrace();
        }
        prodMapper.insertProduct(product);
    }



    //更新商品
    public void editProduct(Product product) {
        String productKey="product_"+product.getProductId();
        try{
            if (redisTemplate.hasKey(productKey)){
                String pJson=MapperUtil.MP.writeValueAsString(product);
                redisTemplate.opsForValue().set(productKey,pJson,2,TimeUnit.DAYS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        prodMapper.updateProduct(product);
    }



}
